/*
Write an encryption program that reads from cin （a file）and writes the encoded characters to cout (a file).
You might use this simple encryption scheme: the encrypted form of a character c is c^key[i], where key is a string passed as a command-line argument. The program uses the characters in key in a cyclic manner until all the input has been read. Re-encrypting encoded text with the same key produces the original text. If no key (or a null string) is passed, then no encrption is done.*/

#include <iostream>
#include <fstream>
using namespace std;

void reader(string* strP,string* keyP){
    cout<<"Please enter the message:";
    cin>>*strP;
    cout<<"Please enter the key:";
    cin>>*keyP;
}

void readFile(const string fileName,string* strP,string* keyP){
    ifstream file;
    file.open(fileName,ios::in);
    file>>*strP;
    file>>*keyP;
    file.close();
}

string encrypt(const string str,const string key){
    if (key=="") return str;

    int sLen = str.length();
    int kLen = key.length();
    char re[sLen]={};
    for (int i = 0; i < sLen; i++)
    {
        re[i] = str[i]^key[i%kLen];
    }
    return re;
}

void writer (const string str,const string key,const string cipher){
    cout<<"-----------------"<<endl;
    cout<<"String:\t"<<str<<endl;
    cout<<"Key:\t"<<key<<endl;
    cout<<"Cipher:\t"<<cipher<<endl;
}

void writeFile(const string fileName,const string str,const string key,const string cipher){
    ofstream file;
    file.open(fileName,ios::out);
    file<<"String:\t"<<str<<endl;
    file<<"Key:\t"<<key<<endl;
    file<<"Cipher:\t"<<cipher<<endl;
    file.close();
}

int main(int argc, char const *argv[])
{
    string str;
    string key="";
    string cipher;

    // readFile("in.txt",&str,&key);
    reader(&str,&key);
    
    cipher=encrypt(str,key);

    // writeFile("out.txt",str,key,cipher);
    writer(str,key,cipher);

    return 0;
}
